package com.example.service.impl;

import org.springframework.stereotype.Service;
import com.example.service.TokenStoreService;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.time.Duration;

public class TokenStoreServiceImpl implements TokenStoreService {

    private static final String KEY_TOKEN_PREFIX = "auth:token:"; // auth:token:<userId>

    private final StringRedisTemplate redisTemplate;

    public TokenStoreServiceImpl(StringRedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public void saveToken(Long userId, String token, long ttlSeconds) {
        if (userId == null || token == null) return;
        redisTemplate.opsForValue().set(KEY_TOKEN_PREFIX + userId, token, Duration.ofSeconds(ttlSeconds));
    }

    @Override
    public String getToken(Long userId) {
        if (userId == null) return null;
        return redisTemplate.opsForValue().get(KEY_TOKEN_PREFIX + userId);
    }

    @Override
    public void deleteToken(Long userId) {
        if (userId == null) return;
        redisTemplate.delete(KEY_TOKEN_PREFIX + userId);
    }
}


